package cn.itcast.service.system.impl;

import cn.itcast.dao.system.UserDao;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiniu.util.Md5;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Autowired
    HttpSession httpSession;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public PageInfo<User> findPages(int pagesNum, int pages,String id) {
        PageHelper.startPage(pagesNum,pages);
        List<User> list = findAllById(id);
        return new PageInfo<>(list,5);
    }

    @Override
    public List<User> findAllById(String id) {
        return userDao.findAllById(id);
    }

    @Override
    public void insert(User user) {
        String password = user.getPassword();//加密之前的密码
        String password1 = user.getPassword();
        //将加密之前的密码放入sessino域对象中
        password= new Md5Hash(password, user.getEmail(), 2).toString();//加密之后的密码
        user.setPassword(password);
        userDao.insert(user);
        Map<String,String> map=new HashMap<>();
        map.put("to",user.getEmail());
        map.put("subject","入住saas平台通知");
        map.put("content","欢迎加入saas-export平台，你现在可以使用邮箱和密码登录项目了，密码是："+password1);
        amqpTemplate.convertAndSend("mail.send",map);
/*        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = null;
        mimeMessageHelper = new MimeMessageHelper(mimeMessage);

        try {
            mimeMessageHelper.setFrom("songshuangzhang@sina.com");
        mimeMessageHelper.setTo(user.getEmail());
        mimeMessageHelper.setSubject("入住saas平台通知");
        mimeMessageHelper.setText("欢迎加入saas-export平台，你现在可以使用邮箱和密码登录项目了，密码是"+password1);
        //mimeMessageHelper.addAttachment("czx.jpg",new File("C:\\Users\\DELL\\Desktop\\czx.jpg"));
        javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }*/
    }

    @Override
    public void deleteById(String id) {
        //删除关联表数据pe_role_user
        userDao.deleteRoleByUserId(id);
        userDao.deleteById(id);
    }

    @Override
    public User findUserById(String id) {
        return userDao.findUserById(id);
    }

    @Override
    public void update(User user) {
        String password = user.getPassword();
        password=new Md5Hash(password,user.getEmail(),2).toString();
        user.setPassword(password);
        userDao.update(user);
    }

    @Override
    public List<String> findAllRoleIdByUserId(String id) {
        return userDao.findAllRoleIdByUserId(id);
    }

    @Override
    public void insertRoleIdByUserId(String id, String roleId) {
        userDao.insertRoleIdByUserId(id,roleId);
    }

    @Override
    public void deleteRoleByUserId(String userid) {
        userDao.deleteRoleByUserId(userid);
    }

    @Override
    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }

    public static void main(String[] args) {
        String s1 = new Md5Hash("123", "laowang@export.com", 2).toString();
        String s2 = new Md5Hash("123", "admin@export.com", 2).toString();
        String s3 = new Md5Hash("123", "xiaoer@export.com", 2).toString();
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
    }
}
